package com.example.smartall.diaglog;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.smartall.R;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

/**
 * 网络设置弹窗 后来才知道自己端口和IP俩个概念搞反了
 * 我把UI界面标识改了一下，但是内在程序还是用反的
 * 改是不可能的，除非重构
 */
public class NetSetDialog extends Dialog {
    private EditText etPort;
    private EditText etIp;
    private Button cancel;
    private Button ok;
    private Context context;
    //    private OnClick onClick;
    private SharedPreferences preferences;

    public NetSetDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    //    public void setOnClick(OnClick onClick) {
//        this.onClick = onClick;
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(context).inflate(R.layout.set_port_dialog, null);
        setContentView(view);
        etPort = (EditText) view.findViewById(R.id.et_port);
        etIp = (EditText) view.findViewById(R.id.et_ip);
        cancel = (Button) view.findViewById(R.id.cancel);
        ok = (Button) view.findViewById(R.id.ok);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NetSetDialog.this.dismiss();
            }
        });
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (onClick != null){
//                    onClick.onOkClickListener();
//                }
                SPUtils spUtils = new SPUtils(context, Constant.SP_NET_FILE_NAME);
                spUtils.putString(Constant.NET_PORT, etPort.getText().toString());
                spUtils.putString(Constant.NET_IP, etIp.getText().toString());
                Toast.makeText(context, "设置成功", Toast.LENGTH_SHORT).show();
                ApiConfig.BASE_URL += spUtils.getString(Constant.NET_PORT)+":"+spUtils.getString(Constant.NET_IP);
                NetSetDialog.this.dismiss();
            }
        });
    }

    public interface OnClick {
        void onOkClickListener();
    }
}
